﻿/**
 * 池底操作已经叫牌事件
 */

function betEvent() {
	this._quene = [];
	this._active = false;
	/**
	 * 調用本類的實例方法
	 * @param	r_room	房間對象
	 * @param	r_user	操作者
	 * @param	r_type	操作類型
	 * @param	r_bet	操作籌碼
	 * @return	.....
	 */
	if ( betEvent.prototype.handleFun == undefined ) {
		betEvent.prototype.handleFun = function(r_room, r_user, r_type, r_bet) {			
			var _handle = { r:r_room.getId(), u:r_user.getUserId(), bet:r_bet , type:r_type };			
			if (r_type == '') return;
			trace("function : " + r_type + " & chip : " + r_bet);		
			return this[r_type](r_room, r_user, r_bet);
			//暂时不用队列
			if (this._active==true) {
				this._quene.push(_handle);
			}else {
				this._action == true;
				return this[r_type](r_room, r_user, r_bet);
			}
		}
	}
	/**
	 * 初始彩池，並返回彩池堆
	 * @param	r_room	遊戲房間
	 */
	if ( betEvent.prototype.formatPot == undefined ) {
		betEvent.prototype.formatPot = function(r_room) {
			var _new = r_room.getVariable("news").value;
			var _index_obj = r_room.getVariable("index");
			var _index_arr = r_room.properties.get("indexArr");
			_index_arr = (_index_arr == null) ? Array() : _index_arr;
			var _index = (_index_obj == null) ? 0 : parseInt(_index_obj.value);
			_index++;
			_index = (_index >= _index_arr.length) ? 0 : _index;
			//開始發牌
			if ( _new != _index_arr[_index] ) {
				trace("formatPot is wrony");
				//return;
			}
			var _user_arr = r_room.properties.get("userArr");
			if (_user_arr == null) {
				return null;
			}
			_user_arr = sortArrByObj(_user_arr, 'num', 1);			
			var _bet_arr = r_room.properties.get("potArr");//房間彩池數組
			_bet_arr = (_bet_arr == undefined ) ? [] : _bet_arr;
			var _pot = parseInt(r_room.getVariable("pot").value);
			var _pot_arr = [];
			while (_user_arr.length) {
				var _temp = this.creatPot(_user_arr);
				if (_temp != null) {
					_user_arr = _temp[0];
					_pot_arr.push(_temp[1]);
				}else {
					_user_arr = null;
				}
			}
			//没有生成池底
			if (_pot_arr == null) {
				return null;
			}
			//遍历池底并判断是否可以合并
			var _pot_merge = false;
			for each(var _potObj in _pot_arr) {
				for ( var m in _bet_arr ) {
					var _temp_bet = new pot(_bet_arr[m][0], _bet_arr[m][1]);
					_pot_merge = _temp_bet.checkMerge(_potObj['user']);
					if ( _pot_merge == true) {
						_bet_arr[m][1] += _potObj['pot'];				
						break;
					}
				}				
				//没有合并
				if (_pot_merge == false) {
					var _temp_bet = new pot(_potObj['user'], _potObj['pot']);
					_bet_arr.push(_temp_bet.getVal());
				}
			}
			setRoomVariable(r_room, { pot:0, call:0 } , false);	
			setRoomProVar(r_room, { potArr:_bet_arr, userArr:[] }, false);			
			return _bet_arr;			
		}
	}
	/**
	 * 池底递归生成
	 * @param _user_arr	池底玩家押注信息
	 */
	if ( betEvent.prototype.creatPot == undefined) {
		betEvent.prototype.creatPot = function(_user_arr) {		
			var _call = 0;
			var _pot = 0;
			var _user_list = [];
			var _user_id_list = [];
			for (var i = 0; i < _user_arr.length; i++ ) {
				var _user_data = _user_arr[i];
				var _data = { };
				_data['userId'] = _user_data['userId'];
				_data['type'] = _user_data['type'];
				_data['num'] = _user_data['num'];
				//弃牌
				if (_user_data['type'] == 'fold') {
					_pot += (_user_data['num'] > _call) ? _call : _user_data['num'];
					_pot += (_call == 0) ? _user_data['num'] : 0;
					if (_call != 0) {
						_data['num'] = parseInt(_user_data['num'] - _call);
						_user_list.push(_data);
					}
					continue;
				}
				//allIn
				if (_user_data['type'] == 'allIn') {
					_call = (_call == 0) ? parseInt(_user_data['num']) : _call;
					var _last = parseInt(_user_data['num']) - _call;
					_pot += _call;
					if ( _last > 0 ) {
						_data['num'] = _last;
						_user_list.push(_data);
					}
					_user_id_list.push(_user_data['userId']);
					continue;
				}
				//其他
				//if ( _user_data['type'] == 'call' || _user_data['type'] == '') {
					_call = (_call == 0) ? parseInt(_user_data['num']) : _call;
					_pot += _call;
					var _last = parseInt(_user_data['num']) - _call;
					if ( _last > 0 ) {
						_data['num'] = _last;
						_user_list.push(_data);						
					}
					_user_id_list.push(_user_data['userId']);
					continue;
				//}	
			}
			_user_id_list = arraySort(_user_id_list, 'asc');
			var _potObj = [_user_list, { pot:_pot, user:_user_id_list } ];			
			return _potObj;
		}
	}
	/**
	 * 玩家下注或者大小盲注
	 * @param	r_room	遊戲房間
	 * @param	r_user	遊戲玩家
	 * @param	r_bet	下注籌碼
	 * @return	int		下注後的池底
	 */
	if ( betEvent.prototype.bet == undefined ) {
		betEvent.prototype.bet = function(r_room, r_user, r_bet) {
			
			
			trace("******************************************")
			var _call = parseInt(r_room.getVariable("call").value);//最大下注	
			var _user_arr = r_room.properties.get("userArr");//玩家下注記錄
			var _index = parseInt(r_room.getVariable("index").value);//當前下注
			var _pot = parseInt(r_room.getVariable("pot").value);
			var _blinds = parseInt(r_room.getVariable("minBlind").value);//當前小盲注
			var _index_arr = r_room.properties.get("indexArr");
			var _user_flag = r_user.getPlayerIndex();			
			var _next = false;
			if (_pot >= _blinds) {
				_next = true;
			}
			_pot += r_bet;				
			if ( _blinds != r_bet ) {
				_call = r_bet;					
			}
			if ( _user_arr == null ) {
				_user_arr = [];
			}
			if ( _user_arr[_user_flag] == undefined ) {
				_user_arr[_user_flag] = { userId:r_user.getUserId(), type:'bet', num:r_bet };					
			}else {
				_user_arr[_user_flag].num = r_bet;
				_user_arr[_user_flag].type = 'bet';
			}
			r_room.properties.put("userArr", _user_arr);
			var _param = { call:_call, pot:_pot };				
			setRoomVariable(r_room, _param, false);				
			userBet(r_user, r_bet, 0);
			var _result = { };
			_result['chip'] = r_bet;
			_result['type'] = 'bet';
			_result['next'] = _next;
			return _result;			
		}
	}
	/**
	 * 跟注
	 * @param	r_room	遊戲房間
	 * @param	r_user	遊戲玩家
	 * @param	r_bet	遊戲籌碼
	 * @return	int		下注後的池底
	 */	
	if ( betEvent.prototype.call == undefined ) {
		betEvent.prototype.call = function(r_room, r_user, r_bet) {			
			var _call = parseInt(r_room.getVariable("call").value);//最大下注	
			var _user_arr = r_room.properties.get("userArr");//玩家下注記錄
			var _pot = parseInt(r_room.getVariable("pot").value);//當前底池
			var _blinds = parseInt(r_room.getVariable("minBlind").value);//當前盲注
			var _user_flag = r_user.getPlayerIndex();
			var _bank = parseInt(r_user.getVariable("bank").value);			
			var _has_bet = (_user_arr[_user_flag] == undefined) ? 0 : _user_arr[_user_flag].num;
			//var _user_bet = ( _user_arr[_user_flag] == undefined ) ? r_bet : parseInt(_has_bet) + r_bet;
			//以下为测试数据
			var _user_bet = _call;
			r_bet = _call - _has_bet;
			if (r_bet >= _bank) {
				return this.allIn(r_room, r_user, _bank);
			}
			if ( _user_bet == _call ) {
				_pot += r_bet;
				_user_arr = (_user_arr == null) ? [] : _user_arr;				
				if ( _user_arr[_user_flag] == undefined ) {
					_user_arr[_user_flag] = { userId:r_user.getUserId(), type:'call', num:_user_bet };					
				}else {
					_user_arr[_user_flag].num = _user_bet;
					_user_arr[_user_flag].type = 'call';
				}
				r_room.properties.put("userArr", _user_arr);
				_bank = _bank - r_bet;
				userBet(r_user, r_bet, 0);
				setRoomVariable(r_room, { pot:_pot }, false);	
				var _result = { };
				_result['chip'] = r_bet;
				_result['type'] = 'call';
				_result['bank'] = _bank;
				return _result;
			}else if ( _user_bet < _call ) {
				return false;
			}		
		}
	}
	/**
	 * 加注
	 * @param	r_room	遊戲房間
	 * @param	r_user	遊戲玩家
	 * @param	r_bet	下注籌碼
	 * @return	int		下注後的池底
	 */
	if ( betEvent.prototype.raise == undefined ) {
		betEvent.prototype.raise = function(r_room, r_user, r_bet) {
			var _call = parseInt(r_room.getVariable("call").value);//最大下注
			var _user_arr = r_room.properties.get("userArr");//玩家下注記錄
			var _pot = parseInt(r_room.getVariable("pot").value);//當前底池
			var _blinds = parseInt(r_room.getVariable("minBlind").value);//當前盲注
			var _bank = parseInt(r_user.getVariable("bank").value);
			if (_bank <= r_bet) {//把桌上的筹码全下了即为allIn
				return this.allIn(r_room, r_user, _bank);
			}
			var _user_flag = r_user.getPlayerIndex();
			var _user_bet = ( _user_arr[_user_flag] == undefined ) ? r_bet : parseInt(_user_arr[_user_flag].num) + r_bet;
			if ( _user_bet > _call ) {
				_call = _user_bet;
				_user_arr = (_user_arr == null) ? Array() : _user_arr;
				var _new = _user_flag;
				if ( _user_arr[_user_flag] == undefined ) {
					_user_arr[_user_flag] = { userId:r_user.getUserId(), type:'raise', num:_user_bet };
				}else {
					_user_arr[_user_flag].num = _user_bet;
					_user_arr[_user_flag].type = 'raise';
				}
				_pot += r_bet;
				_bank = _bank - r_bet;
				r_room.properties.put("userArr", _user_arr);
				var _param = { call:_call, pot:_pot, news:_new };				
				userBet(r_user, r_bet, 0);
				setRoomVariable(r_room, _param, false);	
				var _result = { };
				_result['chip'] = r_bet;
				_result['type'] = 'raise';
				_result['bank'] = _bank;
				return _result;
			}else if ( _user_bet == _call ) {
				return this.call(r_room,r_user,r_bet);
			}else if ( _user_bet < _call ) {
				_user_bet += _bank;
				if (_user_bet >= _call) {
					return this.allIn(r_room, r_user, _user_bet);
				}
				//杯具的开始
				return false;
			}
		}
	}
	/**
	 * 棄牌
	 * @param	r_room	遊戲房間
	 * @param	r_user	遊戲玩家
	 * @param	r_bet	下注籌碼
	 * @return	int		下注後的池底
	 */
	if ( betEvent.prototype.fold == undefined ) {
		betEvent.prototype.fold = function(r_room, r_user, r_bet) {	
			var _user_arr = r_room.properties.get("userArr");//玩家下注記錄			
			var _pot_arr = r_room.properties.get("potArr");
			var _pot = parseInt(r_room.getVariable("pot").value);//當前底池
			var _user_flag = r_user.getPlayerIndex();
			if ( _user_arr[_user_flag] != undefined ) {
				_user_arr[_user_flag].type = 'fold';
			}else {
				_user_arr[_user_flag] = { userId:r_user.getUserId(), type:'fold', num:r_bet };
			} 
			if (_pot_arr != null) {
				for ( var i in _pot_arr ) {
					var _temp_pot = new pot(_pot_arr[i][0],_pot_arr[i][1]);
					_temp_pot.fold(r_user.getUserId());
					_pot_arr[i] = _temp_pot.getVal();
				}
			}
			var _index_arr = r_room.properties.get("indexArr");			
			var _index = r_user.getPlayerIndex();
			//弃牌的时候要考虑到用户掉线时不是他叫牌的情况
			for (var i = 0; i < _index_arr.length; i++ ) {
				if (_index_arr[i] == _index) {
					_index_arr.splice(i, 1);
					break;
				}
			}			
			var _fold = r_room.properties.get("fold");
			_fold = (_fold == null) ? Array() : _fold;
			_fold.push(r_user.getPlayerIndex());
			__userEvent.setPreUserBet(r_room);
			var _param = { fold:_fold, userArr:_user_arr , potArr:_pot_arr, indexArr:_index_arr };			
			setRoomProVar(r_room, _param, false);	
			var _result = { };
			_result['chip'] = 0;
			_result['type'] = 'fold';
			return _result;
		}
	}
	/**
	 * 讓牌
	 * @param	r_room	遊戲房間
	 * @param	r_user	遊戲玩家
	 * @param	r_bet	下注籌碼
	 * @return	int		下注後的池底
	 */
	if ( betEvent.prototype.check == undefined ) {
		betEvent.prototype.check = function(r_room,r_user,r_bet) {			
			var _news = r_room.getVariable("news").value;
			var _pot = parseInt(r_room.getVariable("pot").value);//當前底池
			var _call = parseInt(r_room.getVariable("call").value);//當前最大叫住
			var _user_arr = r_room.properties.get("userArr");
			var _user_flag = r_user.getPlayerIndex();
			var _result = { };
			if (_call == 0 && _pot == 0) {			
				_user_arr = (_user_arr == null) ? Array() : _user_arr;
				_user_arr[_user_flag] = { userId:r_user.getUserId(), type:'check', num:r_bet };
				setRoomProVar(r_room, { userArr:_user_arr }, false);
				_result['chip'] = 0;
				_result['type'] = 'check';
				return _result;
			}else {
				trace(" check wrony");
				var res = this.call(r_room, r_user, _call);
				return res;
			}			
		}		
	}
	/**
	 * all in
	 * @param	r_room	遊戲房間
	 * @param	r_user	遊戲玩家
	 * @param	r_bet	下注籌碼
	 * @return	int		下注後的池底 
	 */
	if ( betEvent.prototype.allIn == undefined ) {
		betEvent.prototype.allIn = function(r_room, r_user, r_bet) {	
			trace("allIn");
			var _call = parseInt(r_room.getVariable("call").value);//最大下注	
			var _user_arr = r_room.properties.get("userArr");//玩家下注記錄
			var _pot = parseInt(r_room.getVariable("pot").value);//當前底池
			var _user_flag = r_user.getPlayerIndex();
			var _bank = parseInt(r_user.getVariable("bank").value);
			r_bet = (r_bet >= _bank) ? _bank : r_bet;
			var _user_bet = ( _user_arr[_user_flag] == undefined ) ? r_bet : parseInt(_user_arr[_user_flag].num) + r_bet;
			var _result = { };
			var _param = { };			
			if ( _user_arr[_user_flag] == undefined ) {
				_user_arr[_user_flag] = { userId:r_user.getUserId(), type:'allIn', num:_user_bet };
			}else {
				_user_arr[_user_flag].num = _user_bet;
				_user_arr[_user_flag].type = 'allIn';
			}
			_pot += r_bet;
			if (_call < _user_bet) {
				_param['call'] = _user_bet;
				_param['news'] = _user_flag;
			}
			var _index_arr = r_room.properties.get("indexArr");
			var _index = parseInt(r_room.getVariable("index").value);
			_index_arr.splice(_index, 1);
			_param['pot'] = _pot;
			//_param['bank'] = _bank - r_bet;
			if (_index_arr.length == 1) {
				_param['just'] = true;
			}
			_bank = _bank - r_bet;		
			setRoomVariable(r_room, _param, false);		
			var _allIn = r_room.properties.get("allIn");
			_allIn = (_allIn == null) ? Array() : _allIn;
			_allIn.push(r_user.getPlayerIndex());			
			__userEvent.setPreUserBet(r_room);
			_param = { userArr:_user_arr, allIn:_allIn , indexArr:_index_arr };	
			setRoomProVar(r_room, _param, false);
			userBet(r_user, r_bet, 0);
			_result['chip'] = r_bet;
			_result['bank'] = _bank;
			_result['type'] = 'allIn';
			return _result;
		}
	}	
}
/**
 * 执行队列
 */
betEvent.prototype.doQuene = function(r_begin) {
	if (r_begin) {
		this._quene.shift();
	}
	var _handle = this._quene.slice(0, 1);
	if (_handle) {
		var _room = _zone.getRoom(_handle['r']);
		var _user = _server.getUserById(_handle['u']);
		return this[_handle['type']](_room, _user, _handle['bet']);
	}else {
		this._active = false;
	}
}